package com.github.ompc.carrying.server.test;

import com.github.ompc.carrying.common.domain.Row;
import com.github.ompc.carrying.common.exception.CarryingException;
import com.github.ompc.carrying.common.service.CarryingService;
import junit.framework.Assert;
import org.junit.Test;

import javax.annotation.Resource;

/**
 * Created by vlinux on 14-9-2.
 */
public class CarryingServiceTestCase extends SpringTestCase {

    @Resource(name="carryingServiceTarget")
    private CarryingService carryingService;

    /**
     * inc token
     * @param token
     * @return
     */
    private int incToken(int token) {
        return (token << 1) | 0xfa;
    }

    @Test
    public void test_carrying_1G_total_10577650() {

        long counter = 0;
        int token = 0;
        boolean isReTry = false;
        Row lastRow = null;
        while(true) {

            try {
                final Row row = carryingService.carry(incToken(token), isReTry);
                if( isReTry ) {
                    Assert.assertEquals(row.getLineNum(), lastRow.getLineNum());
                    Assert.assertEquals(row.getData().length, lastRow.getData().length);
                }

                if( null == row ) {
                    break;
                }

                if( row.getLineNum() % 100 == 0
                        && !isReTry ) {
                    lastRow = row;
                    throw new IllegalStateException("test for exception");
                }

                isReTry = false;
                counter++;
                token++;

                if( counter > 10577650L ) {
                    System.out.println("counter");
                }
            } catch (IllegalStateException e) {
                isReTry = true;
            } catch (CarryingException e) {
                isReTry = true;
            }
        }//while
        Assert.assertEquals(counter, 10577650L);
    }

}
